package com.ynkbny.web.controller;

import com.github.pagehelper.PageInfo;
import com.ynkbny.domain.JobAndTrigger;
import com.ynkbny.entity.AppQuartz;
import com.ynkbny.service.BaseJob;
import com.ynkbny.service.IJobAndTriggerService;
import com.ynkbny.util.ClassUtil;
import com.ynkbny.util.QuartzUtil;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
*@author zelin.wang
*@description 
*@date 7/18/2023
*/
@Api(tags = "任务调度接口")
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    IJobAndTriggerService iJobAndTriggerService;

    @Autowired
    private QuartzUtil quartzUtil;

    @GetMapping(value = "/queryjob")
    public Map<String, Object> queryjob(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
        PageInfo<JobAndTrigger> jobAndTrigger = iJobAndTriggerService.getJobAndTriggerDetails(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("JobAndTrigger", jobAndTrigger);
        map.put("number", jobAndTrigger.getTotal());
        return map;
    }
    @PutMapping(value = "/addJob")
    public Res addJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.addJob(appQuartz);
            return Res.ok("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Res.fail("添加失败");
        }
    }

    @RequestMapping(value = "/modifyJob", method = RequestMethod.PUT)
    public boolean modifyJob(@RequestBody AppQuartz appQuartz){
        try {
            quartzUtil.modifyJob(appQuartz);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/deleteJob", method = RequestMethod.DELETE)
    public boolean deleteJob(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobDelete(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/jobPause", method = RequestMethod.PUT)
    public boolean jobPause(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobPause(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "/jobResume", method = RequestMethod.PUT)
    public boolean jobResume(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            quartzUtil.jobResume(jobName, jobGroup);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping(value = "jobList",method = RequestMethod.GET)
    public Map<String,String> jobList() throws IOException, ClassNotFoundException {
        final String BASE_PACKAGE = "com.ynkbny.tasks";
        /**
         * 扫描tasks包下继承了basejob.class的所有类
         */
        return ClassUtil.loadClass(BASE_PACKAGE, BaseJob.class);
    }

    public boolean checkJob(@RequestBody AppQuartz appQuartz){
        try{
            return quartzUtil.checkJob(appQuartz);
        }catch (Exception e){

        }
        return false;
    }
}
